package factory.one.controller;

import com.alibaba.druid.util.StringUtils;
import factory.one.base.Constant;
import factory.one.pojo.common.Page;
import factory.one.service.UserService;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author sk.z
 * 2023/8/2
 * 用户管理
 **/
@Controller
@RequestMapping("/user")
public class UserController {

    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("/list.html")
    public String listHtml(@RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "10") Integer size,
                           Model model) {
        List<UserDetails> list = userService.list(page, size);
        model.addAttribute("page", new Page<UserDetails>(list, userService.listCount(), page, size));
        return "user/list";
    }

    @GetMapping("/add.html")
    public String addHtml() {
        return "user/add";
    }

    @ResponseBody
    @PostMapping("/add")
    public String add(@RequestParam String userName,
                      @RequestParam String password,
                      @RequestParam String[] roles) {
        userService.createUser(User.withUsername(userName).password(password).roles(roles).build());
        return "true";
    }

    @GetMapping("/edit.html")
    public String editHtml(String userName, Model model) {
        UserDetails userDetails = userService.loadUserByUsername(userName);
        Set<String> set = new HashSet<>();
        for (GrantedAuthority g : userDetails.getAuthorities()) {
            set.add(g.getAuthority());
        }
        model.addAttribute("userDetails", userDetails);
        model.addAttribute("isSale", set.contains(Constant.ROLE_SALE));
        model.addAttribute("isWorker", set.contains(Constant.ROLE_WORKER));
        model.addAttribute("isFinance", set.contains(Constant.ROLE_FINANCE));
        return "user/edit";
    }

    @ResponseBody
    @PostMapping("/edit")
    public String edit(@RequestParam String userName,
                       @RequestParam String password,
                       @RequestParam String[] roles) {
        if (StringUtils.isEmpty(password) || password.length() > 32) {
            userService.updateUserAuthority(User.withUsername(userName).password(userName).roles(roles).build());
        } else {
            userService.updateUser(User.withUsername(userName).password(password).roles(roles).build());
        }
        return "true";
    }

    @GetMapping("/changePassword.html")
    public String changePasswordHtml(){
        return "user/changePassword";
    }

    @ResponseBody
    @PostMapping("/changePassword")
    public String changePassword(String old, String password, String confirm){
        if (!password.equals(confirm)){
            return "false";
        }
        userService.changePassword(old,password);
        return "user";
    }


}
